﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PeachLite.Helpers
{
    public static class UIHelper
    {
        /// <summary>
        /// 生成页码html
        /// </summary>
        /// <param name="size">页大小</param>
        /// <param name="index">当前页码</param>
        /// <param name="rcount">记录总行数</param>
        /// <param name="limt">页码限制</param>
        /// <param name="q">查询字符串</param>
        /// <returns>页码html</returns>
        public static string GetPager(int size, int index, int rcount, int limt, string q)
        {
            int cmax;
            int i;

            //获得总页数
            var pc = (rcount % size > 0) ? rcount / size + 1 : rcount / size;
            if (pc < 1)
            {
                return "";
            }
            string pager = index == 1 ? "<a class='prev dis'>上一页</a>" : "<a class='prev' href='?i=" + (index - 1).ToString() + ((q == null || q == "") ? "" : "&" + q) + "'>上一页</a>";

            if (index - limt <= 1)
            {
                i = 1;
            }
            else
            {
                i = index - limt;
                pager += "<a class='dis'>...</a>";
            }

            cmax = index + limt > pc ? pc : index + limt;

            while (i <= cmax)
            {
                pager += i == index ? "<a class='cur'>" + i.ToString() + "</a>" : "<a href='?i=" + i.ToString() + ((q == null || q == "") ? "" : "&" + q) + "'>" + i.ToString() + "</a>";
                i++;
            }

            pager += index + limt < pc ? "<a class='dis'>...</a>" : "";

            pager += index == pc ? "<a class='next dis'>下一页</a> " : "<a class='next' href='?i=" + (index + 1).ToString() + ((q == null || q == "") ? "" : "&" + q) + "'>下一页</a>";
            return pager;
        }
    }
}